import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/src/extension_navigation.dart';
import 'package:nft_shop/my/page/notice/home_announcement_page.dart';
import 'package:nft_shop/my/page/set/help/model/HelpDetailModel.dart';
import 'package:nft_shop/my/page/set/help/model/HelpModel.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/status_view.dart';

///帮助中心
class MyHelpCenter extends StatefulWidget {
  const MyHelpCenter({Key? key}) : super(key: key);

  @override
  _MyHelpCenterState createState() => _MyHelpCenterState();
}

final backImg = 'assets/images/ic_back_black.png';

class _MyHelpCenterState extends State<MyHelpCenter> {
  List<ProblemList>? _list;

  int status = LoadStatusType.loading;

  @override
  void initState() {
    super.initState();
    _getProblemData();
  }

  ///从服务端获取信息
  void _getProblemData() async {
    try {
      HelpData? helpData = await ServiceRepository.getQuestionList();
      if (helpData != null &&
          helpData.list != null &&
          helpData.list!.length > 0) {
        _list = helpData.list;
        status = LoadStatusType.success;
      } else {
        status = LoadStatusType.empty;
      }
    } catch (e, s) {
      status = LoadStatusType.empty;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        backgroundColor: Color(0xFF2A2A2A),
        backImgColor: Colors.white,
        title: "",
      ),
      body: ListView(children: [
        MyHelpTopWidget(),
        Visibility(
          visible: status != LoadStatusType.success,
          child: Container(
            margin: EdgeInsets.only(top: 100),
            child: StatusViews(status),
          ),
        ),
        Visibility(
          visible: status == LoadStatusType.success,
          child: ListView.builder(
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: _list?.length ?? 0,
              itemBuilder: (context, index) {
                return itemView(_list![index]);
              }),
        ),
      ]),
    );
  }
}

///常见问题item
Widget itemView(ProblemList item) {
  return Container(
    decoration: BoxDecoration(
        border:
            Border(bottom: BorderSide(color: Color(0xffF2F2F2), width: 0.8))),
    padding: EdgeInsets.all(14),
    child: Row(
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 20.0, right: 20.0),
          child: Column(
            children: [
              item.img_url == null || item.img_url!.isEmpty
                  ? Icon(Icons.report_problem_outlined)
                  : LoadImage(
                      item.img_url ?? "",
                      width: 70,
                    ),
              Container(
                alignment: Alignment.center,
                width: 100,
                child: Padding(
                  padding: const EdgeInsets.all(5.0),
                  child: Text(
                    item.name ?? "",
                    style: TextStyle(fontSize: 13, color: Color(0xff333333)),
                  ),
                ),
              )
            ],
          ),
        ),
        Expanded(
            child: Column(
          children: [
            for (int i = 0; i < item.son!.length; i++)
              ProblemItem(
                item.son![i].id ?? -1,
                item.son![i].title!,
                i == item.son!.length - 1,
              )
          ],
        ))
      ],
    ),
  );
}

//常见问题顶部
class MyHelpTopWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
            width: double.infinity,
            padding: EdgeInsets.only(left: 14, right: 14, top: 14),
            color: Color(0xFF2A2A2A),
            child: Stack(
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("帮助中心",
                        style: TextStyle(fontSize: 40, color: Colors.white)),
                    Padding(
                      padding: EdgeInsets.only(top: 10, bottom: 14),
                      child: Text("如需帮助请联系在线客服",
                          style: TextStyle(fontSize: 12, color: Colors.white)),
                    )
                  ],
                ),
                Positioned(
                    bottom: 0,
                    right: 40,
                    child: LoadAssetImage("ic_my_center")),
              ],
            )),
        Container(
          decoration: BoxDecoration(
            color: Color(0xFF2A2A2A),
          ),
          child: Container(
            padding: EdgeInsets.all(14),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(14),
                    topRight: Radius.circular(14))),
            width: double.infinity,
            child: Text(
              "常见问题",
              style: TextStyle(fontSize: 16, color: Color(0xff333333)),
            ),
          ),
        ),
        Divider(
          height: 0.8,
          indent: 14,
          endIndent: 14,
          color: Color(0xffF2F2F2),
        )
      ],
    );
  }
}

///常见问题item
class ProblemItem extends StatefulWidget {
  final String title;
  final int id;
  final bool showBorder;

  ProblemItem(this.id, this.title, this.showBorder);

  @override
  State<ProblemItem> createState() => _ProblemItemState();
}

///常见问题item State
class _ProblemItemState extends State<ProblemItem> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        onItemTap(widget.id);
      },
      child: Row(
        children: [
          Container(
            width: 5,
            height: 5,
            margin: EdgeInsets.only(right: 10),
            decoration: BoxDecoration(
                color: Color(0xffE5E9EC),
                borderRadius: BorderRadius.all(Radius.circular(5))),
          ),
          Expanded(
              child: Container(
            padding: EdgeInsets.only(top: 13, bottom: 13),
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(
                        color: !widget.showBorder
                            ? Color(0xffF2F2F2)
                            : Colors.white,
                        width: 0.8))),
            child: Text(
              widget.title,
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              style: TextStyle(fontSize: 14, color: Color(0xff333333)),
            ),
          ))
        ],
      ),
    );
  }
}

///点击跳转详情
onItemTap(int id) async {
  try {
    EasyLoading.show(status: '加载中...');
    HelpDetaiData? hdm = await ServiceRepository.getNoticeDetail(id);
    EasyLoading.dismiss();
    if (hdm != null &&
        hdm.info != null &&
        hdm.info!.title != null &&
        hdm.info!.content != null) {
      Get.to(AnnouncementPage(
        title: hdm.info!.title ?? "",
        content: hdm.info!.content ?? "",
      ));
    } else {
      EasyLoading.showToast("数据异常");
    }
  } catch (e, s) {
    EasyLoading.showToast("数据异常");
  }
}
